var OrgTreeObj;
var OrgTreeNode = null; //选中机构树节点
var currentCateNode; //选中节点对应的分类节点
var currentOrgNode; //选中节点对应的机构节点
var gridlist;
var selectMaps = {};
var ognid = RequestURLParam.getParam("ognid");
var cate =RequestURLParam.getParam("cate");
$(document).ready(function () {
    gridlist = mini.get('grid');
    OrgTreeObj = mini.get("OrgTree");
    initOrgTree();
});

/***
 * 加载机构树
 * */
function initOrgTree() {
    OrgTreeNode = null;
    Tree.getTree(OrgTreeObj, 'cateOrgView!list.do', "orgId", "orgPid");
}

/***
 * 机构树选择事件
 * */
function OrgTreeselected(e) {
    var OrgTree = e.sender;
    var node = e.node;
    OrgTreeNode = node;
    if (OrgTreeNode.orgType !="cate") {
        if (OrgTreeNode.orgType == 1) {
            currentOrgNode = OrgTreeNode;
            currentCateNode = OrgTree.getParentNode(OrgTreeNode);
            currentOrgId = OrgTreeNode.orgId;
            currentOrgName = OrgTreeNode.orgName;
            PostOrgId = currentOrgId;
            PostDeptId = "";
        } else {
            currentCateNode = OrgTree.getAncestors(OrgTreeNode)[0];
            currentOrgNode = OrgTree.getAncestors(OrgTreeNode)[1];
            currentOrgId = currentOrgNode.orgId;
            currentOrgName = currentOrgNode.orgName;
            PostOrgId = currentOrgId;
            PostDeptId = OrgTreeNode.orgId;
        }
        refreshPostGrid();
    } else {
        PostOrgId = "";
    }
}

/***
 * 刷新岗位列表
 * 参数说明：PostOrgId：单位id；PostDeptId：部门id；PostDutyId：职务id；PostName：岗位名称
 * */
function refreshPostGrid(PostName) {
    var prms = {};
    if (PostOrgId == "") {
        return;
    }
    if (PostOrgId && PostOrgId.trim() != "") {
        prms["orgDutyPostView.ognId"] = PostOrgId;
    }
    if (PostDeptId && PostDeptId.trim() != "") {
        prms["orgDutyPostView.orgId"] = PostDeptId;
    }
    if (PostName && PostName.trim() != "") {
        prms["orgDutyPostView.postNameLike"] = PostName;
    } else {
        $("#Postkey").val("");
    }
    gridlist.load(prms);
}


function selPost() {
    var id = [];
    var name = [];
    var count = 0;
    for (var key in selectMaps) {
        id.push(selectMaps[key].postId);
        name.push(selectMaps[key].postName);
        count++;
    }
    if (count > 0) {
        window.CloseOwnerWindow({
            'id': id.join(","),
            'name': name.join(",")
        });
    } else {
        mini.alert("请勾选!");
    }
}

/*
 * 在表格加载的时候激发事件
 * */
function onGridLoad(e) {
    for (var i = 0; i < gridlist.data.length; i++) {
        for (var key in selectMaps) {
            if (key == gridlist.data[i].postId) {
                gridlist.setSelected(gridlist.getRow(i));
            }
        }
    }
}

/*
 * 在表格选择行时
 * */
function gridclick(e) {
    if (e.records && e.records.length > 0) {
        for (var i = 0; i < e.records.length; i++) {
            delete selectMaps[e.records[i].postId];
        }
    } else {
        delete selectMaps[e.record.postId];
    }
    var rows = gridlist.getSelecteds();
    for (var i = 0; i < rows.length; i++) {
        selectMaps[rows[i].postId] = rows[i];
    }
    int_selectedPost();
}

/*
 * 显示选中的岗位
 */
function int_selectedPost() {
    var Posttitle = "";
    for (var key in selectMaps) {
        Posttitle += '<a class="mini-button mini-button-iconRight" plain="true" style="color:red;" iconCls="icon-no" onclick="PostClear(\'' + key + '\')">' + selectMaps[key].postName + '</a>';
    }
    $("#PostSelected").html(Posttitle);
    mini.parse();
}

/*
 * 删除选中的岗位
 */
function PostClear(key) {
    delete selectMaps[key];
    for (var i = 0; i < gridlist.data.length; i++) {
        if (key == gridlist.data[i].postId) {
            gridlist.deselect(gridlist.getRow(i));
        }
    }
    int_selectedPost();
}